package com.jobsys.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jobsys.dbprocess.Dbjob;
import com.jobsys.interceptor.UserAuthorInterceptor;
import com.jobsys.model.MJobinfo;

/**
 * 添加登录验证
 * 
 * @author pc-12
 *
 */
@Before(UserAuthorInterceptor.class)
public class JobsController extends Controller {
	public void index() {
		// render("/jobsys/jobpage.jsp");
		// redirect("/jobpage.jsp");
		render("/jobpage.jsp");
	}

	/**
	 * page信息
	 * 
	 * @throws ServletException
	 * @throws IOException
	 */
	public void pageBox() throws ServletException, IOException {
		// queryStr,curpage,scity,sindustry,stime,ssource
		String curpage = this.getPara("curpage");
		String queryStr = this.getPara("queryStr");
		String scity = this.getPara("scity");
		String sindustry = this.getPara("sindustry");
		String stime = this.getPara("stime");
		String ssource = this.getPara("ssource");
		String typename = this.getPara("typename");

		JSONObject jb = new JSONObject();
		jb.put("queryStr", queryStr); // 关键词
		jb.put("typename", typename);
		jb.put("scity", scity); // 城市
		jb.put("sindustry", sindustry); // 热点
		jb.put("stime", stime); // 时间
		jb.put("ssource", ssource); // 来源

		if (queryStr == null || queryStr.replaceAll(" ", "").equals("")) {
			queryStr = null;
		}

		if (scity == null || scity.replaceAll(" ", "").equals("") || scity.equals("不限")) {
			scity = null;
		}

		if (sindustry == null || sindustry.replaceAll(" ", "").equals("") || sindustry.equals("不限")) {
			sindustry = null;
		}

		if (stime == null || stime.replaceAll(" ", "").equals("") || stime.equals("不限")) {
			stime = null;
		}

		if (ssource == null || ssource.replaceAll(" ", "").equals("") || ssource.equals("不限")) {
			ssource = null;
		}

		String stime2 = null;
		if (stime != null) {
			int num = 0;
			if (stime.equals("一天内")) {
				num = 0;
			} else if (stime.equals("三天内")) {
				num = 2;
			} else if (stime.equals("一周内")) {
				num = 6;
			} else {
				num = 9;
			}

			SimpleDateFormat st = new SimpleDateFormat("yyyy-MM-dd");
			Calendar ca = Calendar.getInstance();
			ca.add(Calendar.DATE, -num);
			stime2 = st.format(ca.getTime());
		}

		int pageIndex = Integer.parseInt(curpage);
		int pageSize = 30;
		int marketNum = Dbjob.getTotalNums(typename, queryStr, scity, sindustry, stime2, ssource);
		int pages = marketNum / pageSize;
		int remainder = marketNum % pageSize;
		if (remainder > 0) {
			pages++;
		}

		List<Integer> pageArr = new ArrayList<>();

		int lastnum = pages > 9 ? 9 : pages;
		if (pages <= lastnum) {
			for (int i = 1; i <= 9; i++) {
				pageArr.add(i);
			}
		} else {
			if (pageIndex - 4 > 0) {
				int begin = pageIndex - 4;
				lastnum = begin + lastnum > pages ? pages : begin + lastnum;
				if (lastnum - begin < 10) {
					begin = lastnum - 9;
				}

				for (int i = begin; i < lastnum; i++) {
					pageArr.add(i);
				}
			} else {
				for (int i = 1; i <= lastnum; i++) {
					pageArr.add(i);
				}
			}
		}

		jb.put("pages", pages); // 总页数
		jb.put("curpage", pageIndex); // 当前页totalnums
		jb.put("totalnums", marketNum); // 当前页totalnums
		jb.put("pagelist", pageArr);

		JSONObject userinfo = new JSONObject();
		String userID = this.getCookie("userID");
		userinfo.put("userID", userID);

		String right = this.getCookie("right");
		userinfo.put("right", right);

		jb.put("userinfo", userinfo);

		this.renderJson(jb);
	}

	/**
	 * job信息
	 * 
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getjobs() throws ServletException, IOException {
		// queryStr,curpage,scity,sindustry,stime,ssource
		String curpage = this.getPara("curpage");
		String queryStr = this.getPara("queryStr");
		String scity = this.getPara("scity");
		String sindustry = this.getPara("sindustry");
		String stime = this.getPara("stime");
		String ssource = this.getPara("ssource");
		String typename = this.getPara("typename");
		if (queryStr == null || queryStr.replaceAll(" ", "").equals("")) {
			queryStr = null;
		}

		if (scity == null || scity.replaceAll(" ", "").equals("") || scity.equals("不限")) {
			scity = null;
		}

		if (sindustry == null || sindustry.replaceAll(" ", "").equals("") || sindustry.equals("不限")) {
			sindustry = null;
		}

		if (stime == null || stime.replaceAll(" ", "").equals("") || stime.equals("不限")) {
			stime = null;
		}

		if (ssource == null || ssource.replaceAll(" ", "").equals("") || ssource.equals("不限")) {
			ssource = null;
		}

		String stime2 = "";
		if (stime != null) {
			int num = 0;
			if (stime.equals("一天内")) {
				num = 0;
			} else if (stime.equals("三天内")) {
				num = 2;
			} else if (stime.equals("一周内")) {
				num = 6;
			} else {
				num = 9;
			}

			SimpleDateFormat st = new SimpleDateFormat("yyyy-MM-dd");
			Calendar ca = Calendar.getInstance();
			ca.add(Calendar.DATE, -num);
			stime2 = st.format(ca.getTime());
		}

		int pageIndex = Integer.parseInt(curpage);
		int pageSize = 30;

		List<MJobinfo> jobs = Dbjob.getJobinfos(typename, queryStr, pageIndex, pageSize, scity, sindustry, stime2,
				ssource);

		this.renderJson(jobs);
	}
}
